package com.share.netdisk.user.controller;

import com.share.constant.Result;
import com.share.netdisk.user.service.UserService;
import com.share.tdo.UserLoginTdo;
import com.share.tdo.UserRegisterTdo;
import io.swagger.v3.oas.annotations.Operation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RequestMapping("/user")
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //账号密码登录
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public Result login(@RequestBody @Valid UserLoginTdo user){
        log.info("user:{}",user);
        return userService.login(user);
    }

    //手机号登录
    @GetMapping("/login/phone")
    @Operation(summary = "手机号登录", description = "手机号登录")
    public Result loginByPhone(@RequestParam("phone") String phone, @RequestParam("code") String code){
        log.info("phone:{},code:{}",phone,code);
        return userService.loginByPhone(phone,code);
    }



    //注册
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册")
    public Result register(@RequestBody @Valid UserRegisterTdo user){
        log.info("user:{}",user);
        return userService.register(user);
    }

    //根据用户名判断用户是否存在
    @GetMapping("/checkUserName")
    @Operation(summary = "根据用户名判断用户是否存在", description = "根据用户名判断用户是否存在")
    public Result checkUserName(@RequestParam("username") String username){
        log.info("username:{}",username);
        return userService.isExist(username);
    }

    //获取验证码
    @GetMapping("/getCaptcha")
    @Operation(summary = "获取验证码", description = "获取验证码")
    public ResponseEntity<byte[]> getCaptcha(){
        return userService.generateCaptcha();
    }


    //注销
    @GetMapping("/logout")
    @Operation(summary = "用户注销", description = "用户注销")
    public Result logout(){
        return userService.logout();
    }

    //忘记密码
    @PostMapping("/forgetPassword")
    @Operation(summary = "忘记密码", description = "忘记密码")
    public Result forgetPassword(@RequestParam("phone") String phone, @RequestParam("code") String code){
        log.info("phone:{},code:{}",phone,code);
        return userService.loginByPhone(phone,code);
    }

    //获取用户信息
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public Result getUserInfo(){
        return  userService.getUserInfo();
    }

    //获取手机验证码
    @GetMapping("/getPhoneCaptcha")
    public String getPhoneCaptcha(@RequestParam("phone") String phone){
       return userService.getCaptcha(phone);
    }
}
